*This do file prepares generates indices to reduce the number of tested hypothesis

if ("`swdLocal'\Data"=="") {
	di as error "Please run init.do first."
	error 1
}

*Open file
use "${gsdAnalysisOutput}/bscie_panel_regressions", clear
drop if replacement == 1 & gender == 1

*** 1. Employment and income ***************************************************

* REplace employment_variables if person is unemployed
foreach var of varlist count_ls_wage ls_wage_hrs ls_cash_amt ls_wait_amt ls_earn_total count_ls_selfemp ls_selfemp_hrs se_cash_amt se_kind_amt se_wait_amt se_earn_total emp_num biz_revenue biz_sales_yest costs_total{
replace `var' = 0 if `var' ==. & ls_employed == 0
}

*Replace missing values
egen famnonmiss=rownonmiss(count_ls_wage ls_wage_hrs ls_cash_amt ls_wait_amt ls_earn_total count_ls_selfemp ls_selfemp_hrs se_cash_amt se_kind_amt se_wait_amt se_earn_total emp_num biz_revenue biz_sales_yest costs_total ), strok
foreach var of varlist count_ls_wage ls_wage_hrs ls_cash_amt ls_wait_amt ls_earn_total count_ls_selfemp ls_selfemp_hrs se_cash_amt se_kind_amt se_wait_amt se_earn_total emp_num biz_revenue biz_sales_yest costs_total  {
	if famnonmiss!=0{
	foreach num of numlist 1 3 4 5 6 {
		quietly sum `var' if treatment_stream==`num' & wave==2 & final_sample==1
		replace `var'=`r(mean)' if mi(`var') & treatment_stream==`num' 
		}
	}
}


*Generate normalized variables
foreach var of varlist count_ls_wage ls_wage_hrs ls_cash_amt ls_wait_amt ls_earn_total  count_ls_selfemp ls_selfemp_hrs se_cash_amt se_kind_amt se_wait_amt se_earn_total emp_num biz_revenue biz_sales_yest costs_total {
	quietly sum `var' if treatment==0 & wave==2 & final_sample==1
	local mean = `r(mean)'
	local sd = `r(sd)'
	display `mean'
	display `sd'
	gen n_`var'= (`var'-`mean')/ `sd'
}

* Generate variance-covariance matrix
mat accum cov = n_count_ls_wage n_ls_wage_hrs n_ls_cash_amt n_ls_wait_amt n_ls_earn_total n_count_ls_selfemp n_ls_selfemp_hrs n_se_cash_amt n_se_kind_amt n_se_wait_amt n_se_earn_total n_emp_num n_biz_revenue n_biz_sales_yest n_costs_total if wave==2 & treatment==0, noconstant deviations
mat cov = cov/(`r(N)'-1)
mat list cov
* Test that it was done right
corr n_count_ls_wage n_ls_wage_hrs n_ls_cash_amt n_ls_wait_amt n_ls_earn_total n_count_ls_selfemp n_ls_selfemp_hrs n_se_cash_amt n_se_kind_amt n_se_wait_amt n_se_earn_total n_emp_num n_biz_revenue n_biz_sales_yest n_costs_total if wave==2 & treatment==0, cov
* Generate the inverse matrix
mat cov_inv=syminv(cov)
mat list cov_inv
* Generate weights
mat A=J(rowsof(cov_inv),1,1)
mat list A
mat W1=cov_inv*A
mat list W1
*Generate sum of all weights
mat B=J(1,rowsof(W1),1)
matrix W=B*W1
scalar W_all=1/W[1,1]

* Generate indicator
gen employment_index=( W1[1,1]*n_count_ls_wage + W1[2,1]*n_ls_wage_hrs + W1[3,1]*n_ls_cash_amt + W1[4,1]*n_ls_wait_amt + W1[5,1]*n_ls_earn_total + W1[6,1]*n_count_ls_selfemp + W1[7,1]*n_ls_selfemp_hrs + W1[8,1]*n_se_cash_amt + W1[9,1]*n_se_kind_amt + W1[10,1]*n_se_wait_amt + W1[11,1]*n_se_earn_total + W1[12,1]*n_emp_num + W1[13,1]*n_biz_revenue + W1[14,1]*n_biz_sales_yest + W1[15,1]*n_costs_total)
quietly sum employment_index if wave==2 & treatment==0
replace employment_index=employment_index/`r(sd)'
label var employment_index "Employment index"
drop famnonmiss

*Generate standardized variables
foreach var of varlist employment_index {
	quietly sum `var' if treatment==0 & wave==2 & final_sample == 1
	local mean = `r(mean)'
	local sd = `r(sd)'
	display `mean'
	display `sd'
	replace `var'= (`var'-`mean')/ `sd'
}


*** 2. Consumption *************************************************************
*Replace missing values
egen famnonmiss=rownonmiss(food_calc food_spend_total food_grow_total nonfood_spend_total asset_spend_total), strok
foreach var of varlist food_calc food_spend_total food_grow_total nonfood_spend_total asset_spend_total  {
	if famnonmiss!=0{
	foreach num of numlist 1 3 4 5 6 {
		quietly sum `var' if treatment_stream==`num' & wave==2 & final_sample==1
		replace `var'=`r(mean)' if mi(`var') & treatment_stream==`num' & wave==2
		}
	}
}

*Generate standardized variables
foreach var of varlist food_calc food_spend_total food_grow_total nonfood_spend_total asset_spend_total {
	quietly sum `var' if treatment==0 & wave==2 & final_sample==1
	local mean = `r(mean)'
	local sd = `r(sd)'
	display `mean'
	display `sd'
	gen n_`var'= (`var'-`mean')/ `sd'
}

* Generate variance-covariance matrix
mat accum cov = n_food_calc n_food_spend_total n_food_grow_total n_nonfood_spend_total n_asset_spend_total if wave==2 & treatment==0, noconstant deviations
mat cov = cov/(`r(N)'-1)
mat list cov
* Test that it was done right
corr food_calc n_food_calc n_food_spend_total n_food_grow_total n_nonfood_spend_total n_asset_spend_total if wave==2 & treatment==0, cov
* Generate the inverse matrix
mat cov_inv=syminv(cov)
mat list cov_inv
* Generate weights
mat A=J(rowsof(cov_inv),1,1)
mat list A
mat W1=cov_inv*A
mat list W1
*Generate sum of all weights
mat B=J(1,rowsof(W1),1)
matrix W=B*W1
scalar W_all=1/W[1,1]

* Generate indicator
gen consumption_index=( W1[1,1]*n_food_calc + W1[2,1]*n_food_spend_total + W1[3,1]*n_food_grow_total + W1[4,1]*n_nonfood_spend_total + W1[5,1]*n_asset_spend_total)
quietly sum consumption_index if wave==2 & treatment==0
replace consumption_index=consumption_index/`r(sd)'
label var consumption_index "Consumption index"
drop famnonmiss

*** 3. Savings, investment and debt ********************************************

** Generate total number of training sectors**
order train_sector_12mo_*, after(train_12mo) 
egen train_12mo_calc=rowtotal(train_sector_12mo_1-train_sector_12mo_1000)
label var train_12mo_calc "Number of trainings done in past 12 months"

*Recode debt variables
foreach var of varlist loan_win_num debts_other infloan_win_num formal_loan_amt_total informal_loan_amt_total {
	replace `var'=-1*`var'
}

*Replace missing values
egen famnonmiss=rownonmiss(account_yn save_yn bank_amt loan_win_num debts_other infloan_win_num formal_loan_amt_total informal_loan_amt_total nowbusiness train_12mo train_12mo_calc), strok
foreach var of varlist account_yn save_yn bank_amt loan_win_num debts_other infloan_win_num formal_loan_amt_total informal_loan_amt_total nowbusiness train_12mo train_12mo_calc  {
	if famnonmiss!=0{
	foreach num of numlist 1 3 4 5 6 {
		quietly sum `var' if treatment_stream==`num' & wave==2 & final_sample==1
		replace `var'=`r(mean)' if mi(`var') & treatment_stream==`num' & wave==2
		}
	}
}

foreach var of varlist account_yn save_yn bank_amt loan_win_num debts_other infloan_win_num formal_loan_amt_total informal_loan_amt_total nowbusiness train_12mo train_12mo_calc  {
	if famnonmiss!=0{
	foreach num of numlist 1 3 4 5 6 {
		cap quietly sum `var' if treatment_stream==`num' & wave==1 & final_sample==1
		cap replace `var'=`r(mean)' if mi(`var') & treatment_stream==`num' & wave==1
		}
	}
}

*Generate standardized variables
foreach var of varlist account_yn save_yn bank_amt loan_win_num debts_other infloan_win_num formal_loan_amt_total informal_loan_amt_total nowbusiness train_12mo train_12mo_calc {
	quietly sum `var' if treatment==0 & wave==2
	local mean = `r(mean)'
	local sd = `r(sd)'
	display `mean'
	display `sd'
	gen n_`var'= (`var'-`mean')/ `sd'
}

* Generate variance-covariance matrix
mat accum cov = n_account_yn n_save_yn bank_amt n_loan_win_num n_debts_other n_infloan_win_num n_formal_loan_amt_total n_informal_loan_amt_total n_nowbusiness n_train_12mo n_train_12mo_calc if wave==2 & treatment==0, noconstant deviations
mat cov = cov/(`r(N)'-1)
mat list cov
* Test that it was done right
corr n_account_yn n_save_yn bank_amt n_loan_win_num n_debts_other n_infloan_win_num n_formal_loan_amt_total n_informal_loan_amt_total n_nowbusiness n_train_12mo n_train_12mo_calc if wave==2 & treatment==0, cov
* Generate the inverse matrix
mat cov_inv=syminv(cov)
mat list cov_inv
* Generate weights
mat A=J(rowsof(cov_inv),1,1)
mat list A
mat W1=cov_inv*A
mat list W1
*Generate sum of all weights
mat B=J(1,rowsof(W1),1)
matrix W=B*W1
scalar W_all=1/W[1,1]

* Generate indicator
gen savings_debt_index=( W1[1,1]*n_account_yn + W1[2,1]*n_save_yn + W1[3,1]*n_bank_amt + W1[4,1]*n_loan_win_num + W1[5,1]*n_debts_other + W1[6,1]*n_infloan_win_num + W1[7,1]*n_formal_loan_amt_total + W1[8,1]*n_informal_loan_amt_total + W1[9,1]*n_nowbusiness  + W1[10,1]*n_train_12mo + W1[11,1]*n_train_12mo_calc) 

* Generate indicator for baseline
replace savings_debt_index=( W1[1,1]*n_account_yn + W1[2,1]*n_save_yn + W1[3,1]*n_bank_amt + W1[4,1]*n_loan_win_num + W1[5,1]*n_debts_other + W1[6,1]*n_infloan_win_num + W1[7,1]*n_formal_loan_amt_total + W1[8,1]*n_informal_loan_amt_total  + W1[10,1]*n_train_12mo + W1[11,1]*n_train_12mo_calc) if wave == 1



quietly sum savings_debt_index if wave==2 & treatment==0 & final_sample==1
replace savings_debt_index=savings_debt_index/`r(sd)'
label var savings_debt_index "Savings, investment and debt index"
drop famnonmiss

*Recode debt variables
foreach var of varlist loan_win_num debts_other infloan_win_num formal_loan_amt_total informal_loan_amt_total {
	replace `var'=-1*`var'
}

*Generate standardized variables
foreach var of varlist savings_debt_index {
	quietly sum `var' if treatment==0 & wave==2 & final_sample == 1
	local mean = `r(mean)'
	local sd = `r(sd)'
	display `mean'
	display `sd'
	replace `var'= (`var'-`mean')/ `sd'
}



*** 4. Business Skills ********************************************

**Generate index for frequency of business practices****************************
egen bizact_index=rowmean(bizact_comp bizact_cust bizact_saletarget bizact_compare bizact_rec)
label var bizact_index "Frequency of business practices"

**Generate awareness of good business practices indix***************************
egen formal_index=rowmean(formal_name  formal_fees  formal_operating  formal_payam  formal_taxes  formal_bribes  formal_intermediate)
label var formal_index "Awareness of good business practices index"

**Generate number of good business practices performed**************************
egen bizprac_index=rowtotal(bizprac_name  bizprac_fees  bizprac_operating  bizprac_payam  bizprac_taxes  bizprac_bribes  bizprac_intermediate)
label var bizprac_index "Number of performed good business practices"

*Replace missing values
egen famnonmiss=rownonmiss(bizact_comp bizact_cust bizact_saletarget bizact_compare bizact_rec formal_name  formal_fees  formal_operating  formal_payam  formal_taxes  formal_bribes  formal_intermediate bizprac_name  bizprac_fees  bizprac_operating  bizprac_payam  bizprac_taxes  bizprac_bribes  bizprac_intermediate), strok
foreach var of varlist bizact_comp bizact_cust bizact_saletarget bizact_compare bizact_rec formal_name  formal_fees  formal_operating  formal_payam  formal_taxes  formal_bribes  formal_intermediate bizprac_name  bizprac_fees  bizprac_operating  bizprac_payam  bizprac_taxes  bizprac_bribes  bizprac_intermediate  {
	if famnonmiss!=0{
	foreach num of numlist 1 3 4 5 6 {
		quietly sum `var' if treatment_stream==`num' & wave==2 & final_sample==1
		replace `var'=`r(mean)' if mi(`var') & treatment_stream==`num' & wave==2
		}
	}
}
foreach var of varlist bizact_comp bizact_cust bizact_saletarget bizact_compare bizact_rec formal_name  formal_fees  formal_operating  formal_payam  formal_taxes  formal_bribes  formal_intermediate bizprac_name  bizprac_fees  bizprac_operating  bizprac_payam  bizprac_taxes  bizprac_bribes  bizprac_intermediate  {
	if famnonmiss!=0{
	foreach num of numlist 1 3 4 5 6 {
		quietly sum `var' if treatment_stream==`num' & wave==1 & final_sample==1
		replace `var'=`r(mean)' if mi(`var') & treatment_stream==`num' & wave==1
		}
	}
}

*Generate standardized variables
foreach var of varlist bizact_comp bizact_cust bizact_saletarget bizact_compare bizact_rec  {
	quietly sum `var' if treatment==0 & wave==2 & final_sample==1
	local mean = `r(mean)'
	local sd = `r(sd)'
	display `mean'
	display `sd'
	gen n_`var'= (`var'-`mean')/ `sd'
}

foreach var of varlist formal_name formal_fees  formal_operating  formal_payam  formal_taxes  formal_bribes  formal_intermediate   {
	quietly sum `var' if treatment==0 & wave==2 & final_sample==1
	local mean = `r(mean)'
	local sd = `r(sd)'
	display `mean'
	display `sd'
	gen n_`var'= (`var'-`mean')/ `sd'
}
foreach var of varlist bizprac_name bizprac_fees  bizprac_operating  bizprac_payam  bizprac_taxes  bizprac_bribes  bizprac_intermediate {
	quietly sum `var' if treatment==0 & wave==2 & final_sample==1
	local mean = `r(mean)'
	local sd = `r(sd)'
	display `mean'
	display `sd'
	gen n_`var'= (`var'-`mean')/ `sd'
}


* Generate variance-covariance matrix
mat accum cov = n_bizact_comp n_bizact_cust n_bizact_saletarget n_bizact_compare n_bizact_rec formal_name  n_formal_fees  n_formal_operating  n_formal_payam  n_formal_taxes  n_formal_bribes  n_formal_intermediate n_bizprac_name  n_bizprac_fees  n_bizprac_operating  n_bizprac_payam  n_bizprac_taxes  n_bizprac_bribes  n_bizprac_intermediate if wave==2 & treatment==0, noconstant deviations
mat cov = cov/(`r(N)'-1)
mat list cov
* Test that it was done right
corr n_bizact_comp n_bizact_cust n_bizact_saletarget n_bizact_compare n_bizact_rec formal_name  n_formal_fees  n_formal_operating  n_formal_payam  n_formal_taxes  n_formal_bribes  n_formal_intermediate n_bizprac_name  n_bizprac_fees  n_bizprac_operating  n_bizprac_payam  n_bizprac_taxes  n_bizprac_bribes  n_bizprac_intermediate if wave==2 & treatment==0, cov
* Generate the inverse matrix
mat cov_inv=syminv(cov)
mat list cov_inv
* Generate weights
mat A=J(rowsof(cov_inv),1,1)
mat list A
mat W1=cov_inv*A
mat list W1
*Generate sum of all weights
mat B=J(1,rowsof(W1),1)
matrix W=B*W1
scalar W_all=1/W[1,1]

* Generate indicator
gen bizskills_index=( W1[1,1]*n_bizact_comp + W1[2,1]*n_bizact_cust + W1[3,1]*n_bizact_saletarget + W1[4,1]*n_bizact_compare + W1[5,1]*n_bizact_rec + W1[6,1]*n_formal_name + W1[7,1]*n_formal_fees + W1[8,1]*n_formal_operating + W1[9,1]*n_formal_payam + W1[10,1]*n_formal_taxes + W1[11,1]*n_formal_bribes + W1[12,1]*n_formal_intermediate + W1[13,1]*n_bizprac_name + W1[14,1]*n_bizprac_fees + W1[15,1]*n_bizprac_operating + W1[16,1]*n_bizprac_payam + W1[17,1]*n_bizprac_taxes + W1[18,1]*n_bizprac_bribes + W1[19,1]*n_bizprac_intermediate)
quietly sum bizskills_index if wave==2 & treatment==0 & final_sample==1
replace bizskills_index=bizskills_index/`r(sd)'
label var bizskills_index "Business skills index"
drop famnonmiss


*** 5. Life satisfaction and empowerment ***************************************
** Recode IPC variables
recode ipc_powerful ipc_happen ipc_protect ipc_ahead ipc_above ipc_right (1=6) (2=5) (3=4) (4=3) (5=2) (6=1), generate(ipc_powerful_rec ipc_happen_rec ipc_protect_rec ipc_ahead_rec ipc_above_rec ipc_right_rec)

*Replace missing values
egen famnonmiss=rownonmiss(happy_edu happy_fam happy_job happy_income  happy_house   happy_life  happy_comm  happy_security  happy_Friend self_now hh_now self_5yrs hh_5yr ipc_leader ipc_powerful_rec ipc_friends ipc_happen_rec ipc_protect_rec ipc_ahead_rec ipc_above ipc_right_rec ipc_hardwork ipc_actions allow_clothing decision_bizhome_self  decision_loan_self decision_friend_self decision_job_self decision_travel_self decision_night_self decision_vaccinate_self emp_smallitems_self emp_school_self), strok
foreach var of varlist happy_edu happy_fam happy_job happy_income  happy_house   happy_life  happy_comm  happy_security  happy_Friend self_now hh_now self_5yrs hh_5yr ipc_leader ipc_powerful_rec ipc_friends ipc_happen_rec ipc_protect_rec ipc_ahead_rec ipc_above ipc_right_rec ipc_hardwork ipc_actions allow_clothing decision_bizhome_self  decision_loan_self decision_friend_self decision_job_self decision_travel_self decision_night_self decision_vaccinate_self emp_smallitems_self emp_school_self {
	if famnonmiss!=0{
	foreach num of numlist 1 3 4 5 6 {
		quietly sum `var' if treatment_stream==`num' & wave==2 & final_sample==1
		replace `var'=`r(mean)' if mi(`var') & treatment_stream==`num' & wave==2
		}
	}
}

*Generate standardized variables
foreach var of varlist happy_edu happy_fam happy_job happy_income  happy_house   happy_life  happy_comm  happy_security  happy_Friend self_now hh_now self_5yrs hh_5yr ipc_leader ipc_powerful_rec ipc_friends ipc_happen_rec ipc_protect_rec ipc_ahead_rec ipc_above ipc_right_rec ipc_hardwork ipc_actions allow_clothing decision_bizhome_self  decision_loan_self decision_friend_self decision_job_self decision_travel_self decision_night_self decision_vaccinate_self emp_smallitems_self emp_school_self {
	quietly sum `var' if treatment==0 & wave==2 & final_sample==1
	local mean = `r(mean)'
	local sd = `r(sd)'
	display `mean'
	display `sd'
	gen n_`var'= (`var'-`mean')/ `sd'
}

****
* Generate variance-covariance matrix
mat accum cov= n_happy_edu n_happy_fam n_happy_job n_happy_income  n_happy_house n_happy_life n_happy_comm n_happy_security n_happy_Friend n_self_now n_hh_now n_self_5yrs n_hh_5yr n_ipc_leader n_ipc_powerful_rec n_ipc_friends n_ipc_happen_rec n_ipc_protect_rec n_ipc_ahead_rec n_ipc_above n_ipc_right_rec n_ipc_hardwork n_ipc_actions n_allow_clothing n_decision_bizhome_self  n_decision_loan_self n_decision_friend_self n_decision_job_self n_decision_travel_self n_decision_night_self n_decision_vaccinate_self n_emp_smallitems_self n_emp_school_self if wave==2 & treatment==0, noconstant deviations
mat cov = cov/(`r(N)'-1)
mat list cov
* Test that it was done right
corr n_happy_edu n_happy_fam n_happy_job n_happy_income  n_happy_house n_happy_life n_happy_comm n_happy_security n_happy_Friend n_self_now n_hh_now n_self_5yrs n_hh_5yr n_ipc_leader n_ipc_powerful_rec n_ipc_friends n_ipc_happen_rec n_ipc_protect_rec n_ipc_ahead_rec n_ipc_above n_ipc_right_rec n_ipc_hardwork n_ipc_actions n_allow_clothing n_decision_bizhome_self  n_decision_loan_self n_decision_friend_self n_decision_job_self n_decision_travel_self n_decision_night_self n_decision_vaccinate_self n_emp_smallitems_self n_emp_school_self if wave==2 & treatment==0, cov
* Generate the inverse matrix
mat cov_inv=syminv(cov)
mat list cov_inv
* Generate weights
mat A=J(rowsof(cov_inv),1,1)
mat list A
mat W1=cov_inv*A
mat list W1
*Generate sum of all weights
mat B=J(1,rowsof(W1),1)
matrix W=B*W1
scalar W_all=1/W[1,1]

* Generate indicator
gen psycho_index=( W1[1,1]*n_happy_edu + W1[2,1]*n_happy_fam + W1[3,1]*n_happy_job + W1[4,1]*n_happy_income + W1[5,1]*n_happy_house + W1[6,1]*n_happy_life + W1[7,1]*n_happy_comm + W1[8,1]*n_happy_security + W1[9,1]*n_happy_Friend + W1[10,1]*n_self_now + W1[11,1]*n_hh_now + W1[12,1]*n_self_5yrs + W1[13,1]*n_hh_5yr + W1[14,1]*n_ipc_leader + W1[15,1]*n_ipc_powerful_rec + W1[16,1]*n_ipc_friends + W1[17,1]*n_ipc_happen_rec + W1[18,1]*n_ipc_protect_rec + W1[19,1]*n_ipc_ahead_rec + W1[20,1]*n_ipc_above + W1[21,1]*n_ipc_right_rec + W1[22,1]*n_ipc_hardwork + W1[23,1]*n_ipc_actions + W1[24,1]*n_allow_clothing + W1[25,1]*n_decision_bizhome_self + W1[26,1]*n_decision_loan_self + W1[27,1]*n_decision_friend_self + W1[28,1]*n_decision_job_self + W1[29,1]*n_decision_travel_self + W1[30,1]*n_decision_night_self + W1[31,1]*n_decision_vaccinate_self + W1[32,1]*n_emp_smallitems_self + W1[33,1]*n_emp_school_self) 
quietly sum psycho_index if wave==2 & treatment==0 & final_sample==1
replace psycho_index=psycho_index/`r(sd)'
label var psycho_index "Life satisfaction and empowerment index"
drop famnonmiss

*** 6. Risk preferences ********************************************************

*** a) Risk indicator (survey data)
gen risk2=.
replace risk2=1 if risk_night==4
replace risk2=2 if risk_night==3
replace risk2=3 if risk_night==2
replace risk2=4 if risk_night==1
gen risk4=.
replace risk4=1 if risk_boda==4
replace risk4=2 if risk_boda==3
replace risk4=3 if risk_boda==2
replace risk4=4 if risk_boda==1
gen risk5=.
replace risk5=1 if risk_sex==4
replace risk5=2 if risk_sex==3
replace risk5=3 if risk_sex==2
replace risk5=4 if risk_sex==1
gen risk7=.
replace risk7=1 if risk_50biz==4
replace risk7=2 if risk_50biz==3
replace risk7=3 if risk_50biz==2
replace risk7=4 if risk_50biz==1
gen risk11=.
replace risk11=1 if risk_loan==4
replace risk11=2 if risk_loan==3
replace risk11=3 if risk_loan==2
replace risk11=4 if risk_loan==1
replace risk_net=. if risk_net==-99 | risk_net==-98
replace risk_med=. if risk_med==-99 | risk_med==-98

*Generate normalized variables
foreach var of varlist risk_net  risk2  risk_med  risk4  risk5  risk_safebiz  risk7  risk11 risky_lot_num {
	quietly sum `var' if treatment==0 & wave==2 & final_sample==1
	local mean = `r(mean)' 
	local sd = `r(sd)'
	display `mean'
	display `sd'
	gen n_`var'= (`var'-`mean')/ `sd'
}

*Replace missing values
egen famnonmiss=rownonmiss(risk_net  risk2  risk_med  risk4  risk5  risk_safebiz  risk7  risk11 risky_lot_num), strok
foreach var of varlist n_risk_net  n_risk2  n_risk_med  n_risk4  n_risk5  risk_safebiz  n_risk7  n_risk11 n_risky_lot_num{
	if famnonmiss!=0{
	foreach num of numlist 1 3 4 5 6 {
		quietly sum `var' if treatment_stream==`num' & wave==2 & final_sample==1
		replace `var'=`r(mean)' if mi(`var') & treatment_stream==`num' & wave==2
		}
	}
}
*Generate indicator 
egen risk_survey_index=rowmean(n_risk_net  n_risk2  n_risk_med  n_risk4  n_risk5  n_risk_safebiz  n_risk7  n_risk11 n_risky_lot_num)
label var risk_survey_index "Risk index (Survey data) "
drop famnonmiss


*Replace missing values
egen famnonmiss=rownonmiss(n_risk_net  n_risk2  n_risk_med  n_risk4  n_risk5  n_risk_safebiz n_risk7  n_risk11 n_risky_lot_num), strok
foreach var of varlist n_risk_net  n_risk2  n_risk_med  n_risk4  n_risk5  n_risk_safebiz  n_risk7  n_risk11 n_risky_lot_num  {
	if famnonmiss!=0{
	foreach num of numlist 1 3 4 5 6 {
		quietly sum `var' if treatment_stream==`num' & wave==2 & final_sample==1
		replace `var'=`r(mean)' if mi(`var') & treatment_stream==`num' & wave==2
		}
	}
}

*Label variables
replace risky_lot_num=n_risky_lot_num 
label var risky_lot_num "Preference for risky lotteries"

* Generate variance-covariance matrix
mat accum cov= n_risk_net  n_risk2  n_risk_med  n_risk4  n_risk5  n_risk_safebiz  n_risk7  n_risk11  n_risky_lot_num if wave==2 & treatment==0, noconstant deviations
mat cov = cov/(`r(N)'-1)
mat list cov
* Test that it was done right
corr n_risk_net  n_risk2  n_risk_med  n_risk4  n_risk5  n_risk_safebiz  n_risk7  n_risk11  if wave==2 & treatment==0, cov
* Generate the inverse matrix
mat cov_inv=syminv(cov)
mat list cov_inv
* Generate weights
mat A=J(rowsof(cov_inv),1,1)
mat list A
mat W1=cov_inv*A
mat list W1
*Generate sum of all weights
mat B=J(1,rowsof(W1),1)
matrix W=B*W1
scalar W_all=1/W[1,1]

* Generate indicator
gen risk_index=( W1[1,1]*n_risk_net + W1[2,1]*n_risk2 + W1[3,1]*n_risk_med + W1[4,1]*n_risk4 + W1[5,1]*n_risk5 + W1[6,1]*n_risk_safebiz + W1[7,1]*n_risk7 + W1[8,1]*n_risk11 + W1[9,1]*n_risky_lot_num ) 
quietly sum risk_index if wave==2 & treatment==0 & final_sample==1
replace risk_index=risk_index/`r(sd)'
label var risk_index "Risk index"
drop famnonmiss

*** 7. Trust index *************************************************************

**Generate trust index***
recode trust_advantage (1=5) (2=4) (5=1) (4=2), generate(trust_advantage_rec)
recode trust_lendmoney trust_lendposs (1=4) (2=3) (4=1) (3=2), generate(trust_lendmoney_rec trust_lendpossessions_rec )
replace trust_advantage_rec=trust_advantage_rec*0.8
gen trust_people_rec=trust_people*0.8
gen trust_helpful_rec=trust_help*0.8
label var trust_people_rec "People can be trusted"
label var trust_helpful_rec  "People try to be helpful"
label var trust_lendmoney_rec "Lending money"
label var trust_lendpossessions_rec "Lending possessions"

*Replace missing values
		quietly sum trust_people_rec if treatment_stream==1 & wave==2 & final_sample==1
		replace trust_people_rec=`r(mean)' if mi(trust_people_rec) & treatment_stream==1 & wave==2
		quietly sum trust_people_rec if treatment_stream==3 & wave==2 & final_sample==1
		replace trust_people_rec=`r(mean)' if mi(trust_people_rec) & treatment_stream==3 & wave==2
		quietly sum trust_people_rec if treatment_stream==4 & wave==2 & final_sample==1
		replace trust_people_rec=`r(mean)' if mi(trust_people_rec) & treatment_stream==4 & wave==2
		quietly sum trust_people_rec if treatment_stream==5 & wave==2 & final_sample==1
		replace trust_people_rec=`r(mean)' if mi(trust_people_rec) & treatment_stream==5 & wave==2
		quietly sum trust_people_rec if treatment_stream==6 & wave==2 & final_sample==1
		replace trust_people_rec=`r(mean)' if mi(trust_people_rec) & treatment_stream==6 & wave==2

	
		quietly sum trust_helpful_rec if treatment_stream==1 & wave==2 & final_sample==1
		replace trust_helpful_rec=`r(mean)' if mi(trust_helpful_rec) & treatment_stream==1 & wave==2
		quietly sum trust_helpful_rec if treatment_stream==3 & wave==2 & final_sample==1
		replace trust_helpful_rec=`r(mean)' if mi(trust_helpful_rec) & treatment_stream==3 & wave==2
		quietly sum trust_helpful_rec if treatment_stream==4 & wave==2 & final_sample==1
		replace trust_helpful_rec=`r(mean)' if mi(trust_helpful_rec) & treatment_stream==4 & wave==2
		quietly sum trust_helpful_rec if treatment_stream==5 & wave==2 & final_sample==1
		replace trust_helpful_rec=`r(mean)' if mi(trust_helpful_rec) & treatment_stream==5 & wave==2
		quietly sum trust_helpful_rec if treatment_stream==6 & wave==2 & final_sample==1
		replace trust_helpful_rec=`r(mean)' if mi(trust_helpful_rec) & treatment_stream==6 & wave==2

		quietly sum trust_advantage_rec if treatment_stream==1 & wave==2 & final_sample==1
		replace trust_advantage_rec=`r(mean)' if mi(trust_advantage_rec) & treatment_stream==1 & wave==2
		quietly sum trust_advantage_rec if treatment_stream==3 & wave==2 & final_sample==1
		replace trust_advantage_rec=`r(mean)' if mi(trust_advantage_rec) & treatment_stream==3 & wave==2
		quietly sum trust_advantage_rec if treatment_stream==4 & wave==2 & final_sample==1
		replace trust_advantage_rec=`r(mean)' if mi(trust_advantage_rec) & treatment_stream==4 & wave==2
		quietly sum trust_advantage_rec if treatment_stream==5 & wave==2 & final_sample==1
		replace trust_advantage_rec=`r(mean)' if mi(trust_advantage_rec) & treatment_stream==5 & wave==2
		quietly sum trust_advantage_rec if treatment_stream==6 & wave==2 & final_sample==1
		replace trust_advantage_rec=`r(mean)' if mi(trust_advantage_rec) & treatment_stream==6 & wave==2

		quietly sum trust_lendmoney_rec if treatment_stream==1 & wave==2 & final_sample==1
		replace trust_lendmoney_rec=`r(mean)' if mi(trust_lendmoney_rec) & treatment_stream==1 & wave==2
		quietly sum trust_lendmoney_rec if treatment_stream==3 & wave==2 & final_sample==1
		replace trust_lendmoney_rec=`r(mean)' if mi(trust_lendmoney_rec) & treatment_stream==3 & wave==2
		quietly sum trust_lendmoney_rec if treatment_stream==4 & wave==2 & final_sample==1
		replace trust_lendmoney_rec=`r(mean)' if mi(trust_lendmoney_rec) & treatment_stream==4 & wave==2
		quietly sum trust_lendmoney_rec if treatment_stream==5 & wave==2 & final_sample==1
		replace trust_lendmoney_rec=`r(mean)' if mi(trust_lendmoney_rec) & treatment_stream==5 & wave==2
		quietly sum trust_lendmoney_rec if treatment_stream==6 & wave==2 & final_sample==1
		replace trust_lendmoney_rec=`r(mean)' if mi(trust_lendmoney_rec) & treatment_stream==6 & wave==2

		quietly sum trust_lendpossessions_rec if treatment_stream==1 & wave==2 & final_sample==1
		replace trust_lendpossessions_rec=`r(mean)' if mi(trust_lendpossessions_rec) & treatment_stream==1 & wave==2
		quietly sum trust_lendpossessions_rec if treatment_stream==3 & wave==2 & final_sample==1
		replace trust_lendpossessions_rec=`r(mean)' if mi(trust_lendpossessions_rec) & treatment_stream==3 & wave==2
		quietly sum trust_lendpossessions_rec if treatment_stream==4 & wave==2 & final_sample==1
		replace trust_lendpossessions_rec=`r(mean)' if mi(trust_lendpossessions_rec) & treatment_stream==4 & wave==2
		quietly sum trust_lendpossessions_rec if treatment_stream==5 & wave==2 & final_sample==1
		replace trust_lendpossessions_rec=`r(mean)' if mi(trust_lendpossessions_rec) & treatment_stream==5 & wave==2
		quietly sum trust_lendpossessions_rec if treatment_stream==6 & wave==2 & final_sample==1
		replace trust_lendpossessions_rec=`r(mean)' if mi(trust_lendpossessions_rec) & treatment_stream==6 & wave==2

		quietly sum trust_family if treatment_stream==1 & wave==2 & final_sample==1
		replace trust_family=`r(mean)' if mi(trust_family) & treatment_stream==1 & wave==2
		quietly sum trust_family if treatment_stream==3 & wave==2 & final_sample==1
		replace trust_family=`r(mean)' if mi(trust_family) & treatment_stream==3 & wave==2
		quietly sum trust_family if treatment_stream==4 & wave==2 & final_sample==1
		replace trust_family=`r(mean)' if mi(trust_family) & treatment_stream==4 & wave==2
		quietly sum trust_family if treatment_stream==5 & wave==2 & final_sample==1
		replace trust_family=`r(mean)' if mi(trust_family) & treatment_stream==5 & wave==2
		quietly sum trust_family if treatment_stream==6 & wave==2 & final_sample==1
		replace trust_family=`r(mean)' if mi(trust_family) & treatment_stream==6 & wave==2

		quietly sum trust_friends if treatment_stream==1 & wave==2 & final_sample==1
		replace trust_friends=`r(mean)' if mi(trust_friends) & treatment_stream==1 & wave==2
		quietly sum trust_friends if treatment_stream==3 & wave==2 & final_sample==1
		replace trust_friends=`r(mean)' if mi(trust_friends) & treatment_stream==3 & wave==2
		quietly sum trust_friends if treatment_stream==4 & wave==2 & final_sample==1
		replace trust_friends=`r(mean)' if mi(trust_friends) & treatment_stream==4 & wave==2
		quietly sum trust_friends if treatment_stream==5 & wave==2 & final_sample==1
		replace trust_friends=`r(mean)' if mi(trust_friends) & treatment_stream==5 & wave==2
		quietly sum trust_friends if treatment_stream==6 & wave==2 & final_sample==1
		replace trust_friends=`r(mean)' if mi(trust_friends) & treatment_stream==6 & wave==2

		quietly sum trust_neighbours if treatment_stream==1 & wave==2 & final_sample==1
		replace trust_neighbours=`r(mean)' if mi(trust_neighbours) & treatment_stream==1 & wave==2
		quietly sum trust_neighbours if treatment_stream==3 & wave==2 & final_sample==1
		replace trust_neighbours=`r(mean)' if mi(trust_neighbours) & treatment_stream==3 & wave==2
		quietly sum trust_neighbours if treatment_stream==4 & wave==2 & final_sample==1
		replace trust_neighbours=`r(mean)' if mi(trust_neighbours) & treatment_stream==4 & wave==2
		quietly sum trust_neighbours if treatment_stream==5 & wave==2 & final_sample==1
		replace trust_neighbours=`r(mean)' if mi(trust_neighbours) & treatment_stream==5 & wave==2
		quietly sum trust_neighbours if treatment_stream==6 & wave==2 & final_sample==1
		replace trust_neighbours=`r(mean)' if mi(trust_neighbours) & treatment_stream==6 & wave==2

		quietly sum trust_police if treatment_stream==1 & wave==2 & final_sample==1
		replace trust_police=`r(mean)' if mi(trust_police) & treatment_stream==1 & wave==2
		quietly sum trust_police if treatment_stream==3 & wave==2 & final_sample==1
		replace trust_police=`r(mean)' if mi(trust_police) & treatment_stream==3 & wave==2
		quietly sum trust_police if treatment_stream==4 & wave==2 & final_sample==1
		replace trust_police=`r(mean)' if mi(trust_police) & treatment_stream==4 & wave==2
		quietly sum trust_police if treatment_stream==5 & wave==2 & final_sample==1
		replace trust_police=`r(mean)' if mi(trust_police) & treatment_stream==5 & wave==2
		quietly sum trust_police if treatment_stream==6 & wave==2 & final_sample==1
		replace trust_police=`r(mean)' if mi(trust_police) & treatment_stream==6 & wave==2

		quietly sum trust_ngo if treatment_stream==1 & wave==2 & final_sample==1
		replace trust_ngo=`r(mean)' if mi(trust_ngo) & treatment_stream==1 & wave==2
		quietly sum trust_ngo if treatment_stream==3 & wave==2 & final_sample==1
		replace trust_ngo=`r(mean)' if mi(trust_ngo) & treatment_stream==3 & wave==2
		quietly sum trust_ngo if treatment_stream==4 & wave==2 & final_sample==1
		replace trust_ngo=`r(mean)' if mi(trust_ngo) & treatment_stream==4 & wave==2
		quietly sum trust_ngo if treatment_stream==5 & wave==2 & final_sample==1
		replace trust_ngo=`r(mean)' if mi(trust_ngo) & treatment_stream==5 & wave==2
		quietly sum trust_ngo if treatment_stream==6 & wave==2 & final_sample==1
		replace trust_ngo=`r(mean)' if mi(trust_ngo) & treatment_stream==6 & wave==2

		quietly sum trust_elders if treatment_stream==1 & wave==2 & final_sample==1
		replace trust_elders=`r(mean)' if mi(trust_elders) & treatment_stream==1 & wave==2
		quietly sum trust_elders if treatment_stream==3 & wave==2 & final_sample==1
		replace trust_elders=`r(mean)' if mi(trust_elders) & treatment_stream==3 & wave==2
		quietly sum trust_elders if treatment_stream==4 & wave==2 & final_sample==1
		replace trust_elders=`r(mean)' if mi(trust_elders) & treatment_stream==4 & wave==2
		quietly sum trust_elders if treatment_stream==5 & wave==2 & final_sample==1
		replace trust_elders=`r(mean)' if mi(trust_elders) & treatment_stream==5 & wave==2
		quietly sum trust_elders if treatment_stream==6 & wave==2 & final_sample==1
		replace trust_elders=`r(mean)' if mi(trust_elders) & treatment_stream==6 & wave==2

		quietly sum trust_localgov if treatment_stream==1 & wave==2 & final_sample==1
		replace trust_localgov=`r(mean)' if mi(trust_localgov) & treatment_stream==1 & wave==2
		quietly sum trust_localgov if treatment_stream==3 & wave==2 & final_sample==1
		replace trust_localgov=`r(mean)' if mi(trust_localgov) & treatment_stream==3 & wave==2
		quietly sum trust_localgov if treatment_stream==4 & wave==2 & final_sample==1
		replace trust_localgov=`r(mean)' if mi(trust_localgov) & treatment_stream==4 & wave==2
		quietly sum trust_localgov if treatment_stream==5 & wave==2 & final_sample==1
		replace trust_localgov=`r(mean)' if mi(trust_localgov) & treatment_stream==5 & wave==2
		quietly sum trust_localgov if treatment_stream==6 & wave==2 & final_sample==1
		replace trust_localgov=`r(mean)' if mi(trust_localgov) & treatment_stream==6 & wave==2

		quietly sum trust_goss if treatment_stream==1 & wave==2 & final_sample==1
		replace trust_goss=`r(mean)' if mi(trust_goss) & treatment_stream==1 & wave==2
		quietly sum trust_goss if treatment_stream==3 & wave==2 & final_sample==1
		replace trust_goss=`r(mean)' if mi(trust_goss) & treatment_stream==3 & wave==2
		quietly sum trust_goss if treatment_stream==4 & wave==2 & final_sample==1
		replace trust_goss=`r(mean)' if mi(trust_goss) & treatment_stream==4 & wave==2
		quietly sum trust_goss if treatment_stream==5 & wave==2 & final_sample==1
		replace trust_goss=`r(mean)' if mi(trust_goss) & treatment_stream==5 & wave==2
		quietly sum trust_goss if treatment_stream==6 & wave==2 & final_sample==1
		replace trust_goss=`r(mean)' if mi(trust_goss) & treatment_stream==6 & wave==2
		
		quietly sum t_game_send_wb if treatment_stream==1 & wave==2 & final_sample==1
		replace t_game_send_wb=`r(mean)' if mi(t_game_send_wb) & treatment_stream==1 & wave==2
		quietly sum t_game_send_wb if treatment_stream==3 & wave==2 & final_sample==1
		replace t_game_send_wb=`r(mean)' if mi(t_game_send_wb) & treatment_stream==3 & wave==2
		quietly sum t_game_send_wb if treatment_stream==4 & wave==2 & final_sample==1
		replace t_game_send_wb=`r(mean)' if mi(t_game_send_wb) & treatment_stream==4 & wave==2
		quietly sum t_game_send_wb if treatment_stream==5 & wave==2 & final_sample==1
		replace t_game_send_wb=`r(mean)' if mi(t_game_send_wb) & treatment_stream==5 & wave==2
		quietly sum t_game_send_wb if treatment_stream==6 & wave==2 & final_sample==1
		replace t_game_send_wb=`r(mean)' if mi(t_game_send_wb) & treatment_stream==6 & wave==2

		quietly sum t_game_otherplayer if treatment_stream==1 & wave==2 & final_sample==1
		replace t_game_otherplayer=`r(mean)' if mi(t_game_otherplayer) & treatment_stream==1 & wave==2
		quietly sum t_game_otherplayer if treatment_stream==3 & wave==2 & final_sample==1
		replace t_game_otherplayer=`r(mean)' if mi(t_game_otherplayer) & treatment_stream==3 & wave==2
		quietly sum t_game_otherplayer if treatment_stream==4 & wave==2 & final_sample==1
		replace t_game_otherplayer=`r(mean)' if mi(t_game_otherplayer) & treatment_stream==4 & wave==2
		quietly sum t_game_otherplayer if treatment_stream==5 & wave==2 & final_sample==1
		replace t_game_otherplayer=`r(mean)' if mi(t_game_otherplayer) & treatment_stream==5 & wave==2
		quietly sum t_game_otherplayer if treatment_stream==6 & wave==2 & final_sample==1
		replace t_game_otherplayer=`r(mean)' if mi(t_game_otherplayer) & treatment_stream==6 & wave==2


*Generate standardized variables
foreach var of varlist trust_people_rec trust_helpful_rec trust_advantage_rec trust_lendmoney_rec trust_lendpossessions_rec trust_family trust_friends trust_neighbours trust_police trust_ngo trust_elders trust_localgov trust_goss t_game_send_wb t_game_otherplayer  {
	quietly sum `var' if treatment==0 & wave==2 & final_sample==1
	local mean = `r(mean)'
	local sd = `r(sd)'
	display `mean'
	display `sd'
	gen n_`var'= (`var'-`mean')/ `sd'
}


* Generate variance-covariance matrix
mat accum cov= n_trust_people_rec n_trust_helpful_rec n_trust_advantage_rec n_trust_lendmoney_rec n_trust_lendpossessions_rec n_trust_family n_trust_friends n_trust_neighbours n_trust_police n_trust_ngo n_trust_elders n_trust_localgov n_trust_goss  n_t_game_send_wb n_t_game_otherplayer   if wave==2 & treatment==0, noconstant deviations
mat cov = cov/(`r(N)'-1)
mat list cov
* Test that it was done right
corr n_trust_people_rec n_trust_helpful_rec n_trust_advantage_rec n_trust_lendmoney_rec n_trust_lendpossessions_rec n_trust_family n_trust_friends n_trust_neighbours n_trust_police n_trust_ngo n_trust_elders n_trust_localgov n_trust_goss  n_t_game_send_wb n_t_game_otherplayer  if wave==2 & treatment==0, cov
* Generate the inverse matrix
mat cov_inv=syminv(cov)
mat list cov_inv
* Generate weights
mat A=J(rowsof(cov_inv),1,1)
mat list A
mat W1=cov_inv*A
mat list W1
*Generate sum of all weights
mat B=J(1,rowsof(W1),1)
matrix W=B*W1
scalar W_all=1/W[1,1]

* Generate indicator
gen trust_index=( W1[1,1]*n_trust_people_rec + W1[2,1]*n_trust_helpful_rec + W1[3,1]*n_trust_advantage_rec + W1[4,1]*n_trust_lendmoney_rec + W1[5,1]*n_trust_lendpossessions_rec + W1[6,1]*n_trust_family + W1[7,1]*n_trust_friends + W1[8,1]*n_trust_neighbours + W1[9,1]*n_trust_police + W1[10,1]*n_trust_ngo + W1[11,1]*n_trust_elders + W1[12,1]*n_trust_localgov + W1[13,1]*n_trust_goss) + W1[14,1]*n_t_game_send_wb + W1[15,1]*n_t_game_otherplayer
quietly sum trust_index if wave==2 & treatment==0 & final_sample==1
replace trust_index=trust_index/`r(sd)'
label var trust_index "Trust index"

*** 8. Engagement in crime and violence index **********************************

gen crime_concern=crime_woman if gender==1
replace crime_concern=crime_man if gender==0

** a) Involvement in crime and violence ***
*Replace missing values
egen famnonmiss=rownonmiss(security_self security_freq security_lastweek  cattle_steal cattle_steal_num loot_yesno loot_num harass_1mo harass_1mo_num beaten_yn crime_concern), strok
foreach var of varlist security_self security_freq security_lastweek  cattle_steal cattle_steal_num loot_yesno loot_num harass_1mo harass_1mo_num beaten_yn crime_concern {
	if famnonmiss!=0{
	foreach num of numlist 1 3  4 5 6 {
		quietly sum `var' if treatment_stream==`num' & final_sample==1
		replace `var'=r(mean) if mi(`var') & treatment_stream==`num'
		}
	}
}

*Generate standardized variables
foreach var of varlist security_self security_freq security_lastweek  cattle_steal cattle_steal_num loot_yesno loot_num harass_1mo harass_1mo_num beaten_yn crime_concern {
	quietly sum `var' if treatment==0 & wave==2 & final_sample==1
	local mean = `r(mean)'
	local sd = `r(sd)'
	display `mean'
	display `sd'
	gen n_`var'= (`var'-`mean')/ `sd'
}
* Generate variance-covariance matrix
mat accum cov= n_security_self n_security_freq n_security_lastweek n_cattle_steal n_cattle_steal_num n_loot_yesno n_loot_num n_harass_1mo n_harass_1mo_num n_beaten_yn n_crime_concern if wave==2 & treatment==0, noconstant deviations
mat cov = cov/(`r(N)'-1)
mat list cov
* Test that it was done right
corr n_security_self n_security_freq n_security_lastweek n_cattle_steal n_cattle_steal_num n_loot_yesno n_loot_num n_harass_1mo n_harass_1mo_num n_beaten_yn n_crime_concern if wave==2 & treatment==0, cov
* Generate the inverse matrix
mat cov_inv=syminv(cov)
mat list cov_inv
* Generate weights
mat A=J(rowsof(cov_inv),1,1)
mat list A
mat W1=cov_inv*A
mat list W1
*Generate sum of all weights
mat B=J(1,rowsof(W1),1)
matrix W=B*W1
scalar W_all=1/W[1,1]

* Generate indicator
gen crime_index=( W1[1,1]*n_security_self + W1[2,1]*n_security_freq + W1[3,1]*n_security_lastweek + W1[4,1]*n_cattle_steal + W1[5,1]*n_cattle_steal_num + W1[6,1]*n_loot_yesno + W1[7,1]*n_loot_num + W1[8,1]*n_harass_1mo + W1[9,1]*n_harass_1mo_num) + W1[10,1]*n_beaten_yn + W1[11,1]*n_crime_concern
quietly sum crime_index if wave==2 & treatment==0 & final_sample==1
replace crime_index=crime_index/`r(sd)'
label var crime_index "Crime and violence index"
drop famnonmiss

* b) Generate list experiment cattle index***
egen list_cattle_index=rowmean(list_cattle_1 list_cattle_2)
quietly sum list_cattle_index if treatment==0 & wave==2 & final_sample==1
	local mean = `r(mean)'
	local sd = `r(sd)'
	display `mean'
	display `sd'
	replace list_cattle_index = (list_cattle_index-`mean')/ `sd' if  wave==2
label var list_cattle_index "List experiment cattle index"

* c) Generate list experiment argument index***
egen list_arg_index=rowmean(list_arg_1 list_arg_2 list_arg_3)
quietly sum list_arg_index if treatment==0 & wave==2 & final_sample==1
	local mean = `r(mean)'
	local sd = `r(sd)'
	display `mean'
	display `sd'
	replace list_arg_index = (list_arg_index-`mean')/ `sd' if  wave==2
label var list_arg_index "List experiment argument index"


*** 9. Migration index *********************************************************

recode ssd12m (1=0) (0=1), gen(outsidessd12m)

*Replace missing values
egen famnonmiss=rownonmiss(moved_yn outsidessd12m refugee idp move_want_yn), strok
foreach var of varlist moved_yn outsidessd12m refugee idp move_want_yn  {
	if famnonmiss!=0{
	foreach num of numlist 1 3 4 5 6 {
		quietly sum `var' if treatment_stream==`num' & final_sample==1
		replace `var'=r(mean) if mi(`var') & treatment_stream==`num'
		}
	}
}

*Generate standardized variables
foreach var of varlist moved_yn outsidessd12m refugee idp move_want_yn {
	quietly sum `var' if treatment==0 & wave==2 & final_sample==1
	local mean = `r(mean)'
	local sd = `r(sd)'
	display `mean'
	display `sd'
	gen n_`var'= (`var'-`mean')/ `sd'
}

* Generate variance-covariance matrix
mat accum cov= n_moved_yn n_outsidessd12m n_refugee n_idp n_move_want_yn  if wave==2 & treatment==0, noconstant deviations
mat cov = cov/(`r(N)'-1)
mat list cov
* Test that it was done right
corr n_moved_yn n_outsidessd12m n_refugee n_idp n_move_want_yn if wave==2 & treatment==0, cov
* Generate the inverse matrix
mat cov_inv=syminv(cov)
mat list cov_inv
* Generate weights
mat A=J(rowsof(cov_inv),1,1)
mat list A
mat W1=cov_inv*A
mat list W1
*Generate sum of all weights
mat B=J(1,rowsof(W1),1)
matrix W=B*W1
scalar W_all=1/W[1,1]

* Generate indicator
gen migration_index=( W1[1,1]*n_moved_yn + W1[2,1]*n_outsidessd12m + W1[3,1]*n_refugee + W1[4,1]*n_idp + W1[5,1]*n_move_want_yn) 
quietly sum migration_index if wave==2 & treatment==0 & final_sample==1
replace migration_index=migration_index/`r(sd)'
label var migration_index "Migration index"
drop famnonmiss

********************************************************************************
*Drop normalized variables to avoid confusion
drop n_*

*Copy business skills indicators of baseline as controls
foreach var of varlist bizact_index formal_index bizprac_index {
	clonevar `var'_b =`var'
	replace `var'_b = l.`var'
}

*Save in different file
save "${gsdAnalysisOutput}/bscie_panel_indices_final", replace

